#include "dialog_comm_setting.h"
#include "ui_dialog_comm_setting.h"


Dialog_Comm_setting::Dialog_Comm_setting(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_Comm_setting)
{
    ui->setupUi(this);
    connect(ui->buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()),SLOT(on_ok_clicked()));
}

Dialog_Comm_setting::~Dialog_Comm_setting()
{
    delete ui;
}

void Dialog_Comm_setting::on_pushButton_refresh_clicked()
{
        foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
       {
           QSerialPort serial;
           serial.setPort(info);
           ui->comboBox_Port->addItem(serial.portName());
        }



}

void Dialog_Comm_setting::on_ok_clicked()
{
//    qDebug()<<"hello, new world";
    QString port_info = ui->comboBox_Port->currentText();
    if(port_info == "")
    {
//  example      QMessageBox::information(NULL, "Title","Content",QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        QMessageBox::information(NULL, "Warning","No Com Port!!!");
    }
    else{
        m_port_setting.baud=ui->comboBox_Baud->currentText().toInt();
        m_port_setting.port=ui->comboBox_Port->currentText();
        m_port_setting.dataBits=ui->comboBox_Db->currentText().toInt();
        m_port_setting.responseTime=ui->lineEdit_Response_time->text().toInt();
        m_port_setting.numberOfRetries=ui->lineEdit_Retry->text().toInt();

        switch(ui->comboBox_Parity->currentIndex())
        {
        case 0:
            m_port_setting.parity=QSerialPort::NoParity;
            break;
        case 1:
            m_port_setting.parity=QSerialPort::EvenParity;
            break;
        case 2:
            m_port_setting.parity=QSerialPort::OddParity;
            break;
        default:
            m_port_setting.parity=QSerialPort::NoParity;
            break;
        }

        emit send_port_settings(m_port_setting);

    }


}





